﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfAppBing092702;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        //创建Binding对象
        Binding binding = new Binding()
        {
            //设定binding的数据源
            Source = tbSource,
            //设定需要绑定的数据源的属性名，这里是tbSource中的Text属性
            Path = new PropertyPath("Text"),
            //设定binding的传递方式，这里是双向
            Mode = BindingMode.TwoWay,
            //设定binding的触发器，这里是指源的属性发生变化时，就触发数据传递
            UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
        };
        //第一种绑定方法，采用目标对象的setBinding方法，三个参数：
        //第一个是表示绑定的目标方法中那个控件属性作为目标路径，第二个参数是binding对象
        tbPath.SetBinding(TextBox.TextProperty, binding);

        List<string> stringList = new List<string>()
        {
            "eric1",
            "min", 
            "yun",
            "tong"
        };
        int a = 1;
        //这里可以用插值动态显示这个值
        //注意，如果new Binding("/")指的是List的第0个元素，如果用[n]表示List的第n+1个元素，用插值方式，可以动态显示数据
        //如果是"/[0]"，指的是“eric”中的e
        this.TextBox5.SetBinding(TextBox.TextProperty, new Binding($"[{a}]") { Source = stringList,Mode = BindingMode.OneTime});

        List<Student> students = new List<Student>()
        {
            new Student()
            {
                Name = "eric",
                Age = 47,
                Address = "肇庆市端州区",
            },
            new Student()
            {
                Name = "min",
                Age = 42,
                Address = "肇庆市乐堂村",
            },
            new Student()
            {
                Name = "yun",
                Age = 15,
                Address = "肇庆一实初中部",
            },
            new Student()
            {
                Name = "tong",
                Age = 8,
                Address = "肇庆一实小学部",
            }
        };
  
          //这里的path，就是students对象的第[2]个元素中的Student对象中的address属性值
        // this.TextBox6.SetBinding(TextBox.TextProperty, new Binding("[2].Address") { Source = students,Mode = BindingMode.OneTime});
        //这里的/Address,指默认的第[0]个元素的address属性
        this.TextBox6.SetBinding(TextBox.TextProperty, new Binding("/Address") { Source = students,Mode = BindingMode.OneTime});

        TextBlock1.SetBinding(TextBlock.TextProperty, new Binding("[2].Name") { Source = students });
        


    }
}